<?php
	class videos extends CI_Controller{
		function __construct(){
			parent::__construct();
			$this->load->model(array('video', 'komentar', 'article', 'photo'));
			$this->load->helper(array('form', 'dharma_date_helper', 'dharma_video_helper'));
			$this->userdata = $this->session->userdata('userdata');
			if($this->userdata){
				$recent_articles = $this->article->get_article(NULL, NULL, 5);
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6);
			}else{
				$recent_articles = $this->article->get_article(NULL, NULL, 5, 0, 'public');
				$recent_photos = $this->photo->get_foto(NULL, NULL, 6, 0, 'public');
			}
			$this->right_sidebar = array('categories' => unserialize(ARTICLE_CATEGORY),
										 'recent_articles' => $recent_articles,
										 'recent_photos' => $recent_photos);
		}
		
		public function index(){
			if($this->userdata){
				$data['videos'] = $this->video->get_video();
			}else{
				$data['videos'] = $this->video->get_video(NULL, NULL, NULL, NULL, 'public');
			}
			if($data['videos']){
				foreach($data['videos'] as $video){
					$comment[] = $this->komentar->get_current_komentar('video', $video['id_video'], 'LIST_KOMENTAR');
				}				
			}else $comment = NULL;
			$data['total_comments'] = $comment;
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('video/index', $data);
		}
		
		public function detail($id_video){
			$data['komentars'] = $this->komentar->get_current_komentar('video', $id_video);
			$videos = $this->video->get_video($id_video);
			$data['detail'] = $videos[0];
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('video/detail', $data);
		}
		
		public function video_komentar($id_video){
			echo json_encode($komentar = $this->komentar->get_current_komentar('video', $id_video, NULL, $this->input->post('id_komentar')));
		}
		
		public function insert_komentar(){
			$data_komentar = array('jenis'=>'video',
								   'id_item'=>$this->input->post('id_video'),
								   'isi'=>$this->input->post('isi'),
								   'id_user'=>$this->userdata['id_user']								   
								  );
			$id_komentar = $this->komentar->add($data_komentar);
			echo json_encode($id_komentar);
		}
		
		public function user_videos(){
			$data['videos'] = $this->video->get_video(NULL, $this->userdata['id_user']);
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('video/user_video', $data);
		}
		
		public function add(){
			if(isset($_POST['video_submit'])){
				$post_data = array('id_user' 	=> $this->userdata['id_user'],
								   'judul' 		=> $this->input->post('judul'),
								   'deskripsi' 	=> $this->input->post('deskripsi'),
								   'url' 		=> $this->input->post('url'),
								   'status' 	=> $this->input->post('status')
								   );
				$id = $this->video->add($post_data);
				redirect(site_url('/videos/user_videos'));
			}
			$data['right_sidebar'] = $this->right_sidebar;
			$this->load->view('video/form', $data);
		}
		
		public function edit($id){
			if(isset($_POST['video_submit'])){				
				$post_data = array('id_user' 	=> $this->userdata['id_user'],
								   'judul' 		=> $this->input->post('judul'),
								   'deskripsi' 	=> $this->input->post('deskripsi'),
								   'url' 		=> $this->input->post('url'),
								   'status' 	=> $this->input->post('status')
								   );
				$this->video->edit($id, $post_data);
				redirect(site_url('/videos/user_videos'));
			}
			$data['right_sidebar'] = $this->right_sidebar;
			$data['video'] = $this->video->get_video($id);
			$this->load->view('video/form', $data);
		}
		
		public function delete($id){ 
			$this->video->delete($id);
			$this->komentar->delete($id);
		}
	}
?>